//
//  HiRegionController.m
//  HiCar
//
//  Created by 丁一 on 2019/1/8.
//  Copyright © 2019 丁一. All rights reserved.
//

#import "HiRegionController.h"
#import "HiCodeController.h"
#import "RootSuperAction.h"
#import "ValidatePhoneViewModel.h"

@interface HiRegionController ()<UITextFieldDelegate,ValidatePhoneDelegate>{
    BOOL ChooseOk;
}

@property (nonatomic, strong) NSArray *textarray;

@property (nonatomic, strong) UITextField *textn001;
@property (nonatomic, strong) UITextField *textn002;
@property (nonatomic, strong) UITextField *textn003;

@property (nonatomic, strong) UIButton    *XiyiButton;

@end

@implementation HiRegionController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if (_Type==0) {
        self.navigationItem.title=@"忘记密码";
        _textarray = [[NSArray alloc] initWithObjects:@"请输入手机号",@"请输入新密码",@"请确认密码", nil];
    }else{
        self.navigationItem.title=@"注册";
        _textarray = [[NSArray alloc] initWithObjects:@"请输入手机号",@"请输入密码",@"请确认密码", nil];
    }
    ChooseOk = YES;
    self.view.backgroundColor = LineColor;
    [self loadhere];
}

- (void)loadhere{
    [_textarray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIView *textbg            = [[UIView alloc] initWithFrame:CGRectMake(25, NAV_Height+15+65*idx, RECRWITCH-50, 50)];
        textbg.layer.cornerRadius  = 5;
        textbg.layer.masksToBounds = YES;
        textbg.backgroundColor    = [UIColor whiteColor];
        [self.view addSubview:textbg];
        
        UITextField *textfield    = [[UITextField alloc] initWithFrame:CGRectMake(10, 5, textbg.frame.size.width-20, textbg.frame.size.height-10)];
        textfield.textColor       = [UIColor blackColor];
        textfield.font            = [UIFont systemFontOfSize:17];
        [textfield setValue:[UIFont systemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
        textfield.backgroundColor = [UIColor whiteColor];
        textfield.placeholder     = obj;
        textfield.tag             = idx;
        textfield.delegate        = self;
        textfield.layer.borderWidth = 0;
        [textbg addSubview:textfield];
        if (idx==0) {
            self.textn001             = textfield;
            self.textn001.keyboardType = UIKeyboardTypePhonePad;
        }else if (idx==1){
            self.textn002              = textfield;
            self.textn002.secureTextEntry = YES;
        }else{
            self.textn003              = textfield;
            self.textn003.secureTextEntry = YES;
        }
    }];
    
    UIButton *button           = [[UIButton alloc] initWithFrame:CGRectMake(25, NAV_Height + 215, RECRWITCH-50, 45)];
    button.tag=1;
    [button setTitle:@"下一步" forState:UIControlStateNormal];
    [button setTitleColor:NavItemTitleColor forState:UIControlStateNormal];
    [button.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
    [button addTarget:self action:@selector(go_done:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    _XiyiButton = [[UIButton alloc] initWithFrame:CGRectMake(RECRWITCH/2-40, RECERHEIGHT-35, 13, 13)];
    [_XiyiButton setTitle:@"✓" forState:UIControlStateNormal];
    _XiyiButton.layer.borderWidth=1.1;
    _XiyiButton.layer.borderColor=Buttoncolor.CGColor;
    [_XiyiButton setTitleColor:Buttoncolor forState:UIControlStateNormal];
    [_XiyiButton.titleLabel setFont:[UIFont boldSystemFontOfSize:15]];
    [_XiyiButton addTarget:self action:@selector(chooseok:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_XiyiButton];
    
    UIButton *xiyigo           = [[UIButton alloc] initWithFrame:CGRectMake(RECRWITCH/2-20, RECERHEIGHT-35-6, 60, 25)];
    xiyigo.tag = 10;
    [xiyigo setTitle:@"用户协议" forState:UIControlStateNormal];
    [xiyigo setTitleColor:Buttoncolor forState:UIControlStateNormal];
    [xiyigo.titleLabel setFont:[UIFont systemFontOfSize:13]];
    [xiyigo addTarget:self action:@selector(go_done:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:xiyigo];
}

- (void)chooseok:(UIButton *)sender{
    if (ChooseOk) {
        ChooseOk = NO;
        [_XiyiButton setTitle:@"" forState:UIControlStateNormal];
    }else{
        ChooseOk = YES;
        [_XiyiButton setTitle:@"✓" forState:UIControlStateNormal];
    }
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    
}

- (void)go_done:(UIButton *)sender{
    if (sender.tag==1) {
        if (!ChooseOk) {
            [SVProgressHUD showErrorWithStatus:@"请同意我们的用户协议"];
        }else{
            if (self.textn001.text.length<1||self.textn001.text==nil) {
                [SVProgressHUD showErrorWithStatus:@"请填写手机号"];
            }else if (self.textn002.text.length<1||self.textn002.text==nil){
                [SVProgressHUD showErrorWithStatus:@"请填写密码"];
            }else if (![HuaHereClass checkiphone:self.textn001.text]){
                [SVProgressHUD showErrorWithStatus:@"请填写正确的手机号"];
            }else if (![JonesClass checkPassWord:self.textn002.text]){
                [SVProgressHUD showErrorWithStatus:@"密码格式不对"];
            }else if (![self.textn002.text isEqualToString:self.textn003.text]){
                [SVProgressHUD showErrorWithStatus:@"确认密码不一致"];
            }else{
                NSMutableDictionary *dic=[NSMutableDictionary new];
                [dic setValue:_textn001.text forKey:@"phone"];
                if (_Type==0) {
                    [dic setValue:@(2000) forKey:@"type"];
                }else{
                    [dic setValue:@(1000) forKey:@"type"];
                }
                ValidatePhoneViewModel *viewmodel = [[ValidatePhoneViewModel alloc] init:dic];
                viewmodel.delegate=self;
            }
        }
    }else{
        [RootSuperAction Actiontodone:JonesGOTO_Web view:self dictionary:@{@"url":@"http://47.92.160.124:8053/agressment.html"}];
    }
}

- (void)Succes:(int)value{
    if (value==1) {
        HiCodeController *codevc=[HiCodeController new];
        codevc.Type = _Type;
        codevc.phone = _textn001.text;
        codevc.password = self.textn003.text;
        [self.navigationController pushViewController:codevc animated:YES];
    }
}

@end
